from pyspades.server import block_action
from pyspades.collision import distance_3d_vector
from commands import add, admin
from map import Map
from pyspades.constants import *
from twisted.internet import reactor
import commands

def paint(connection):
    if connection.protocol.paintround:
        connection.protocol.send_chat('Paint mode deactivated.')
        connection.protocol.paintround = False
    else:
        if connection.protocol.paintround is None:
            connection.protocol.paintround = set()
        connection.protocol.send_chat('Paint mode activated.')
        connection.protocol.paintround = True
add(paint)

def apply_script(protocol, connection, config):
    class paintroundConnection(connection):
        def on_block_removed(self, x, y, z):
            if self.protocol.paintround == True and self.tool == 2:
                reactor.callLater(0.25,self.do_paint,x, y, z)
            return connection.on_block_removed(self, x, y, z)
        def do_paint(self, x, y, z):
            block_action.x = x
            block_action.y = y
            block_action.z = z
            block_action.player_id = self.player_id
            block_action.value = BUILD_BLOCK
            self.protocol.send_contained(block_action, save = True)
            self.protocol.map.set_point(x, y, z, self.color+(255,), user = False)
    class paintroundProtocol(protocol):
        paintround = None
    return paintroundProtocol, paintroundConnection